package org.zcl.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.hash.Jackson2HashMapper;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Package org.zcl
 * @PROJECT_NAME: question
 * @DESCRIPTION:
 * @USER: zhuchenglin
 * @DATE: 2022/5/29 14:25
 */
@Component
public class TestRedis {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    ObjectMapper objectMapper;

    public void testRedis() {
        stringRedisTemplate.convertAndSend("ooxx", "hello");
        RedisConnection cc = stringRedisTemplate.getConnectionFactory().getConnection();
        cc.subscribe(new MessageListener() {
            @Override
            public void onMessage(Message message, byte[] bytes) {
                byte[] body = message.getBody();
                System.out.println(new String(body));
            }
        }, "ooxx".getBytes());
        while (true) {
            stringRedisTemplate.convertAndSend("ooxx","hello wo zi ji");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public void testRedis6() {
        Person person = new Person();
        person.setName("z3");
        person.setAge(16);

        Jackson2HashMapper jackson2HashMapper = new Jackson2HashMapper(objectMapper, false);
        stringRedisTemplate.opsForHash().putAll("z301", jackson2HashMapper.toHash(person));
        Map map = stringRedisTemplate.opsForHash().entries("z301");
        Person value = objectMapper.convertValue(map, Person.class);
        System.out.println(value);
    }

    public void testRedis5() {
        Person person = new Person();
        person.setName("z3");
        person.setAge(16);

        //未来使用stringRedisTemplate高阶api的时候，凡是对hash类型，value都会直接写成 json 这种格式来完成序列化
        stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));

        Jackson2HashMapper jackson2HashMapper = new Jackson2HashMapper(objectMapper, false);
        stringRedisTemplate.opsForHash().putAll("z301", jackson2HashMapper.toHash(person));
        Map map = stringRedisTemplate.opsForHash().entries("z301");
        Person value = objectMapper.convertValue(map, Person.class);
        System.out.println(value);
    }

    public void testRedis4() {
        HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
        hash.put("zcl", "name", "zhuchenglin");
        hash.put("zcl", "age", "29");

        System.out.println(hash.entries("zcl"));
    }

    public void testRedis3() {
        // 低阶api
        RedisConnection coon = redisTemplate.getConnectionFactory().getConnection();
        //开发人员手动进行字节数组转换，比较累
        coon.set("hello02".getBytes(), "china".getBytes());
        System.out.println(new String(coon.get("hello02".getBytes())));

        HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
        hash.put("john", "name", "weijiangming");
        hash.put("john", "age", "20");

        System.out.println(hash.entries("john"));
    }

    public void testRedis2() {
        // stringRedisTemplate高阶api,无乱码
        stringRedisTemplate.opsForValue().set("hello01", "china");
        System.out.println(stringRedisTemplate.opsForValue().get("hello"));
    }

    public void testRedis1() {
        // redisTemplate高阶api，乱码序列化
        redisTemplate.opsForValue().set("hello", "china");
        System.out.println(redisTemplate.opsForValue().get("hello"));
    }

}
